package memberrank

import (
	"context"

	"mall/common/globalkey"
	"mall/common/modelx"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/utils/pointy"
	"mall/service/setting/rpc/internal/svc"
	"mall/service/setting/rpc/internal/utils/errorhandler"
	"mall/service/setting/rpc/types/setting"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetMemberRankByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetMemberRankByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMemberRankByIdLogic {
	return &GetMemberRankByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// group: memberrank
func (l *GetMemberRankByIdLogic) GetMemberRankById(in *setting.IDReq) (*setting.MemberRankInfo, error) {
	res, err := l.svcCtx.MemberRank.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	if res.Status != uint8(globalkey.StatusEnabled) {
		return nil, errorx.NewNotFoundError("setting.memberRankNotExists")
	}

	return &setting.MemberRankInfo{
		Id:             &res.ID,
		Name:           &res.Name,
		Code:           &res.Code,
		Description:    &res.Description,
		Remark:         &res.Remark,
		PicId:          &res.PicID,
		PicUrl:         &res.PicURL,
		PointThreshold: pointy.GetPointer(uint32(res.PointThreshold)),
		IsDefault:      pointy.GetPointer(uint32(res.IsDefault)),
		Sort:           pointy.GetPointer(uint32(res.Sort)),
		Status:         pointy.GetPointer(uint32(res.Status)),
		CreatedAt:      pointy.GetPointer(modelx.FormatTime(res.CreatedAt, "")),
		UpdatedAt:      pointy.GetPointer(modelx.FormatTime(res.UpdatedAt, "")),
		DeletedAt:      pointy.GetPointer(modelx.FormatNullDotTime(res.DeletedAt, "")),
	}, nil
}
